package client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import kryonetpackets.KickPacket;
import kryonetpackets.LoginConfirm;
import kryonetpackets.LoginRequest;
import kryonetpackets.MessagePacket;
import kryonetpackets.NetworkPlayer;
import kryonetpackets.PlayerGroupExitedPlanet;
import kryonetpackets.PlayerGroupExitedWorld;
import kryonetpackets.PlayerGroupJoinedPlanet;
import kryonetpackets.PlayerGroupJoinedWorld;
import kryonetpackets.RGB;

import com.badlogic.gdx.Gdx;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.*;
import com.me.mygdxgame.MyGdxGame;

public class GameClient {
	
	private Client client = null;
	private final MyGdxGame game;
	
	public GameClient(final MyGdxGame game) {
		this.game = game;
	}
	
	public void startClient()
	{
		this.client = new Client();
		client.start();
		try {
			client.connect(5000, "127.0.0.1", 8078,8079);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Kryo kryo = client.getKryo();
		kryo.register(LoginRequest.class);
		kryo.register(LoginConfirm.class);
		kryo.register(KickPacket.class);
		kryo.register(PlayerGroupJoinedWorld.class);
		kryo.register(PlayerGroupJoinedPlanet.class);
		kryo.register(PlayerGroupExitedWorld.class);
		kryo.register(PlayerGroupExitedPlanet.class);
		kryo.register(NetworkPlayer.class);
		kryo.register(MessagePacket.class);
		kryo.register(RGB.class);
		kryo.register(List.class);
		kryo.register(ArrayList.class);
		kryo.register(Random.class);
		
		System.out.println("Client started!");
		
		client.addListener(new Listener() {
			   public void received (Connection connection, Object object) {
				   
			      if (object instanceof LoginConfirm) {
			    	  final LoginConfirm loginConfirm = (LoginConfirm) object;
			    	  
			    	  if (loginConfirm.IsLoggedIn()) {
			    		  Gdx.app.postRunnable(new Runnable() {
			    		  	@Override
			    		  		public void run() {
				    			  game.logIn(loginConfirm.getPlayer());
				    		  }
				    	  });
			    	  }
			    	  else if (!loginConfirm.IsLoggedIn()) {
			    		  //TODO Add action for being denied
			    		  System.out.println("Invalid username or password!");
			    	  }
			        
			      }
			      else if (object instanceof KickPacket) {
			    	  connection.close();
			    	  System.out.println("I was kicked!");
			    	  //TODO Add kicked message or dialogue
			      }
			      
			      else if (object instanceof PlayerGroupJoinedWorld)
			      {
			    	  PlayerGroupJoinedWorld m = (PlayerGroupJoinedWorld) object;
			    	  for (NetworkPlayer p : m.getPlayers())
			    	  {
			    	  System.out.println(p.getName() + " joined my world!");
			    	  }
			      }
			      else if (object instanceof PlayerGroupJoinedPlanet)
			      {
			    	  final PlayerGroupJoinedPlanet m = (PlayerGroupJoinedPlanet) object;
			    	  
			    	  for (NetworkPlayer p : m.getPlayers())
			    	  {
			    	  System.out.println(p.getName() + " joined my planet!");
			    	  Gdx.app.postRunnable(new Runnable() {
			    		  @Override
			    		  public void run() {
			    			  game.playerGroupJoinedPlanet(m.getPlayers());
			    		  }
			    	  });
			    	  }
			    	  
			      }
			      else if (object instanceof PlayerGroupExitedWorld)
			      {
			    	  PlayerGroupExitedWorld m = (PlayerGroupExitedWorld) object;
			    	  for (NetworkPlayer p : m.getPlayers())
			    	  {
			    	  System.out.println(p.getName() + " left my world!");
			    	  }
			      }
			      else if (object instanceof PlayerGroupExitedPlanet)
			      {
			    	  final PlayerGroupExitedPlanet m = (PlayerGroupExitedPlanet) object;
			    	  for (NetworkPlayer p : m.getPlayers())
			    	  {
			    	  System.out.println(p.getName() + " left my planet!");
			    	  Gdx.app.postRunnable(new Runnable() {
			    		  @Override
			    		  public void run() {
			    			  game.playerGroupLeftPlanet(m.getPlayers());
			    		  }
			    	  });
			    	  }
			      }
			      else if (object instanceof MessagePacket)
			      {
			    	  final MessagePacket message = (MessagePacket) object;
			    	  
			    	  Gdx.app.postRunnable(new Runnable() {
			    		  @Override
			    		  public void run() {
			    			  game.appendChatBox(message.getName() + ": " + message.getMessage());
			    		  }
			    	  });
			      }
			      
			   }
			   public void disconnected(Connection connection) {
				   game.getGameClient().startClient();
				   Gdx.app.postRunnable(new Runnable() {
					   @Override
					   public void run() {
						   game.initLoginScreen();
					   }
				   });
			   }
			});
		
		
	}
	public void sendPacket(Object object)
	{
		this.client.sendTCP(object);
	}
	public String getThisPlayerName()
	{
		return game.getThisPlayer().getName();
	}
	

}
